require([
	'jquery',
	'module/util',
	'module/popup',
	'global',
	'datatables.fixedcolumns',
	'requirejs/domready!'],
function($, util, popup, g){
	var tag_table = null,
		tag_fc_table = null;
	
	$('.main').on('click', '.toolbar .btn.add', function(){
		$(window).trigger('show.xt.modal', ['#edit-tag-modal']);
	}).on('click', '.toolbar .btn.remove', function(){
		var d = tag_table.row('.selected').data();
		if(!d) return;
		popup({
	    	msg: '确定要删除标签 “' + d.name + '” 吗？',
	    	title: '确认删除',
	    	panel: false,
	    	style: 'modal-md',
	    	buttons: {
				'取消': {
					callback: function(event){
						this.$element.modal('hide');
					}
				},
				'确定': {
					primary: true,
					callback: function(event){
						this.$element.modal('hide');
						$.ajax({
						    type: 'delete',
						    dataType: 'json',
						    url: g.context+ '/admin/tag/'+d.id,
						    contentType: 'application/json; charset=utf-8'
					    }).done(function(result){
							tag_table.columns.adjust();
							tag_table.ajax.reload(null, false);
					    });
					}
				}
			}
	    });
	}).on('click', '.toolbar .btn.edit', function(){
		var d = tag_table.row('.selected').data();
		$(window).trigger('show.xt.modal', ['#edit-tag-modal', d]);
	});
	$(window).on('ok.xt.modal', function(e, modalId){
		if(tag_table && modalId == '#edit-tag-modal'){
			tag_table.columns.adjust();
			tag_table.ajax.reload(null, false);
		}
	});
	// 渲染表格
	$('#tag-table').on('click', 'tbody td', function(){
		var dt = $('#tag-table').dataTable().api();
		var idx = dt.cell(this).index();
		if(!idx) return;
		var row = dt.row(idx.row).data();
		$(this).parents('tbody').find('tr.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		var $ctr = $(this).parents('.DTFC_ScrollWrapper')
			.find('.DTFC_LeftBodyLiner .DTFC_Cloned tbody tr');
		$ctr.filter('.selected').removeClass('selected');
		$($ctr.get(idx.row)).addClass('selected');
	}).on('init.dt', function(){
		tag_fc_table = new $.fn.dataTable.FixedColumns(
    		$('#tag-table').dataTable().api(), {
				"iLeftColumns": 1,
				"iLeftWidth": 220
    		});
		$('#tag-table_wrapper .toolbar').html(
				'<div class="btn-group" role="group">'+
				    '<button type="button" class="btn btn-default add"><i class="fa fa-plus"></i></button>'+
				    '<button type="button" class="btn btn-default remove"><i class="fa fa-times"></i></button>'+
				    '<button type="button" class="btn btn-default edit"><i class="fa fa-edit"></i></button>'+
				'</div>');
    }).on('draw.dt', function(){
    	if(tag_table){
    		tag_table.columns.adjust();
    	}
		if(tag_fc_table){
			tag_fc_table.fnRedrawLayout();
		}
    }).dataTable( {
    	language: {
            url: g.context + '/assets/js/chinese.json'
        },
        dom: '<"row"<"col-xs-6"<"toolbar">><"col-xs-6"f>>rt<"row"<"col-md-4"i><"col-md-8"p>>',
    	processing : true,
	    serverSide : true,
	    scrollX: '100%',
		scrollCollapse : true,
	    ajax: {
	    	type: 'POST',
	    	url: g.context + '/admin/tags',
	    	contentType: 'application/json; charset=utf-8',
	    	global: false,
	    	data: function(d){
	    		return JSON.stringify(d);
	    	},
	    	error: function(xhr, status, error){
	    		console.log(error);
	    	}
	    },
	    columnDefs: [ 
	     	{ targets : ['_all'], defaultContent: '&nbsp;', searchable : false }
	    ],
	    columns: [
	        { data: 'name'},
	        { data: 'alias'},
	        { data: 'createdAt'},
	        { data: 'updatedAt'}
	    ]
    });
	tag_table = $('#tag-table').dataTable().api();
});